import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_sensor_backend/assets/color.dart';
import 'package:flutter_sensor_backend/component/kitx_model.dart';
import 'package:sensors_plus/sensors_plus.dart';

class SensorPreview extends StatefulWidget {
  const SensorPreview({super.key});

  @override
  State<SensorPreview> createState() => _SensorPreviewState();
}

class _SensorPreviewState extends State<SensorPreview> {
  double x = 0.0;
  double y = 0.0;
  double z = 0.0;

  int getNowTime() {
    return DateTime.now().millisecond;
  }

  getData() {
    // int timestamp = getNowTime();
    accelerometerEvents.listen((event) {
      if ((event.x - x > 0.1 || event.x - x < -0.1) &&
          (event.y - y > 0.1 || event.y - y < -0.1) &&
          (event.z - z > 0.1 || event.z - z < -0.1)) {
        try {
          setState(() {
            x = event.x;
            y = event.y;
            z = event.z;
          });
        } catch (e) {}
      }
    });
  }

  @override
  void initState() {
    getData();
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    try {
      return Container(
        width: MediaQuery.of(context).size.width,
        margin: EdgeInsets.symmetric(horizontal: 15),
        padding: EdgeInsets.symmetric(vertical: 30),
        decoration: BoxDecoration(
          color: os_white,
          borderRadius: BorderRadius.circular(15),
        ),
        child: Column(
          children: [
            DeviceRotationDisplayStand(),
            Container(height: 20),
            Text.rich(
              TextSpan(
                style: TextStyle(
                  color: Color(0xff585858),
                ),
                children: [
                  TextSpan(text: "X轴 "),
                  TextSpan(
                    text: x.toStringAsFixed(2),
                    style: TextStyle(
                      color: os_black,
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  TextSpan(text: "   "),
                  TextSpan(text: "Y轴 "),
                  TextSpan(
                    text: y.toStringAsFixed(2),
                    style: TextStyle(
                      color: os_black,
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  TextSpan(text: "   "),
                  TextSpan(text: "Z轴 "),
                  TextSpan(
                    text: z.toStringAsFixed(2),
                    style: TextStyle(
                      color: os_black,
                      fontSize: 14,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      );
    } catch (e) {
      return Container();
    }
  }
}
